<?php require_once('../Connections/gabriel_paris.php'); 
/* if not logged in then back to login page */
  session_start();
  if(!isset($_SESSION['is_successful_login']) || $_SESSION['is_successful_login'] == false){ 
    header ('location: index.php'); exit;
  }

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if ((isset($_GET['id_caracteristica'])) && ($_GET['id_caracteristica'] != "")) {
  $deleteSQL = sprintf("DELETE FROM caracteristicas WHERE id_caracteristica=%s",
                       GetSQLValueString($_GET['id_caracteristica'], "int"));

  mysql_select_db($database_gabriel_paris, $gabriel_paris);
  $Result1 = mysql_query($deleteSQL, $gabriel_paris) or die(mysql_error());

  $deleteGoTo = "barrios_editar.php?id_barrio=".$_GET['id_barrio'];
  
  header(sprintf("Location: %s", $deleteGoTo));
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
  $foto_id = rand(5, 1500);
  $nombre_archivo = $foto_id.$_FILES ['file_upload']['name'];   
  $tipo_archivo = $_FILES['file_upload']['type'];
  $tamano_archivo = $_FILES['file_upload']['size'];	

 if($_FILES ['file_upload']['name']!=""){
  $updateSQL = sprintf("UPDATE barrios SET nombre=%s, titulo=%s, descripcion_breve=%s, descripcion=%s, latitud=%s, longitud=%s, ubicacion=%s, logo=%s, destacado=%s, principal=%s WHERE id_barrio=%s",
                       GetSQLValueString($_POST['nombre'], "text"),
                       GetSQLValueString($_POST['titulo'], "text"),
                       GetSQLValueString($_POST['descripcion_breve'], "text"),
                       GetSQLValueString($_POST['descripcion'], "text"),
                       GetSQLValueString($_POST['latitud'], "double"),
                       GetSQLValueString($_POST['longitud'], "double"),
                       GetSQLValueString($_POST['ubicacion'], "text"),
                       GetSQLValueString($nombre_archivo, "text"),
                       GetSQLValueString(isset($_POST['destacado']) ? "true" : "", "defined","1","0"),
					    GetSQLValueString(isset($_POST['principal']) ? "true" : "", "defined","1","0"),
                       GetSQLValueString($_POST['id_barrio'], "int"));
  }else{
   $updateSQL = sprintf("UPDATE barrios SET nombre=%s,titulo=%s,descripcion_breve=%s,descripcion=%s,latitud=%s,longitud=%s,
										   ubicacion=%s,destacado=%s,principal=%s,orden=%s 
					   WHERE id_barrio=%s",
                       GetSQLValueString($_POST['nombre'], "text"),
                       GetSQLValueString($_POST['titulo'], "text"),
                       GetSQLValueString($_POST['descripcion_breve'], "text"),
                       GetSQLValueString($_POST['descripcion'], "text"),
                       GetSQLValueString($_POST['latitud'], "double"),
                       GetSQLValueString($_POST['longitud'], "double"),
                       GetSQLValueString($_POST['ubicacion'], "text"),
                       GetSQLValueString(isset($_POST['destacado']) ? "true" : "", "defined","1","0"),
					   GetSQLValueString(isset($_POST['principal']) ? "true" : "", "defined","1","0"),
					   GetSQLValueString($_POST['orden'], "int"),
                       GetSQLValueString($_POST['id_barrio'], "int"));	 
 }

  mysql_select_db($database_gabriel_paris, $gabriel_paris);
  $Result1 = mysql_query($updateSQL, $gabriel_paris) or die(mysql_error());

  $updateGoTo = "barrios.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  $marcadeagua="../fotos/marca_de_agua_gparis.png";
  if (is_uploaded_file($_FILES['file_upload']['tmp_name']))
  {		
	$nombre = $_FILES['file_upload']['name'];
	copy($_FILES['file_upload']['tmp_name'],'../fotos/'.$nombre);
	$subio = true;
    $marca=new img();
    $marca->marcadeagua($img, $marcadeagua, $img, 100);
  }
		
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_barrios_editar = "-1";
if (isset($_GET['id_barrio'])) {
  $colname_barrios_editar = $_GET['id_barrio'];
}
mysql_select_db($database_gabriel_paris, $gabriel_paris);
$query_barrios_editar = sprintf("SELECT id_barrio, nombre, titulo, descripcion_breve, descripcion, latitud, longitud,
								        ubicacion, logo, destacado,principal,orden 
								 FROM barrios 
								 WHERE id_barrio = %s",
								 GetSQLValueString($colname_barrios_editar, "int"));
$barrios_editar = mysql_query($query_barrios_editar, $gabriel_paris) or die(mysql_error());
$row_barrios_editar = mysql_fetch_assoc($barrios_editar);
$totalRows_barrios_editar = mysql_num_rows($barrios_editar);

$maxRows_paginas = 10;
$pageNum_paginas = 0;
if (isset($_GET['pageNum_paginas'])) {
  $pageNum_paginas = $_GET['pageNum_paginas'];
}
$startRow_paginas = $pageNum_paginas * $maxRows_paginas;

$colname_paginas = "-1";
if (isset($_GET['id_barrio'])) {
  $colname_paginas = $_GET['id_barrio'];
}
mysql_select_db($database_gabriel_paris, $gabriel_paris);
$query_paginas = sprintf("SELECT id_caracteristica, nombre FROM caracteristicas WHERE id_barrio = %s", GetSQLValueString($colname_paginas, "int"));
$query_limit_paginas = sprintf("%s LIMIT %d, %d", $query_paginas, $startRow_paginas, $maxRows_paginas);
$paginas = mysql_query($query_limit_paginas, $gabriel_paris) or die(mysql_error());
//$row_paginas = mysql_fetch_assoc($paginas);

if (isset($_GET['totalRows_paginas'])) {
  $totalRows_paginas = $_GET['totalRows_paginas'];
} else {
  $all_paginas = mysql_query($query_paginas);
  $totalRows_paginas = mysql_num_rows($all_paginas);
}
$totalPages_paginas = ceil($totalRows_paginas/$maxRows_paginas)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" rel="stylesheet" href="../css/style3.css" />
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="../ckfinder/ckfinder.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
   jQuery("#eliminarImagen").click(function(event){
     jQuery("#img").hide();
	 jQuery("#file_upload").show();
	 jQuery("#eliminarImagen").hide();
	 return false;   
   });
 });

</script>
<title>G. Tavella | Asesores Inmobiliarios</title>
</head>

<body>

<?php include('header.php'); ?>

<div id="container">
<div class="wrapper">

	<div id="contenedor" class="clearfix">
   	  <div class="clearfix">
            <div id="sidebar">
                <?php include('menu_propiedades.php'); ?>

            </div>
            <!-- fin Sidebar -->
            
        <div id="contenido">
          <div class="item">
             <h2>Barrios Cerrados - Edición</h2>
          </div>
          <form action="<?php echo $editFormAction; ?>" method="post"  enctype="multipart/form-data" name="form2" id="form2">
            <table align="left" cellspacing="0">
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Nombre:</td>
                <td><input type="text" name="nombre" value="<?php echo htmlentities($row_barrios_editar['nombre'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right" valign="top">Descripcion Breve:</td>
                <td><textarea name="descripcion_breve" cols="110" rows="5"><?php echo htmlentities($row_barrios_editar['descripcion_breve'], ENT_COMPAT, 'utf-8'); ?></textarea></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Descripcion:</td>
                <td>  <textarea name="descripcion" ><?php echo htmlentities($row_barrios_editar['descripcion'], ENT_COMPAT, 'utf-8'); ?></textarea>
                 <script type="text/javascript">
					    var editor = CKEDITOR.replace( 'descripcion' );
                        CKFinder.setupCKEditor( editor, '../ckfinder/' );        
                 </script>
                </td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Direcci&oacute;n::</td>
                <td>
                 
                 <input type="text" name="ubicacion" value="<?php echo htmlentities($row_barrios_editar['ubicacion'], ENT_COMPAT, 'utf-8'); ?>" size="42" />&nbsp;&nbsp;&nbsp;&nbsp;
                  Orden:&nbsp;&nbsp; <input type="text" name="orden" 
                value="<?php echo htmlentities($row_barrios_editar['orden'], ENT_COMPAT, 'utf-8'); ?>" size="10" />
                </td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Logo:</td>
                <td><input id="file_upload" name="file_upload" type="file" style="display:none"  />
                  <span id="img">
                  <img src="<?php echo "../fotos/".$row_barrios_editar['logo']; ?>" />
                  </span> &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" id="eliminarImagen">Eliminar Imagen</a> &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp;
                  </td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Destacado:</td>
                <td><input type="checkbox" name="principal" value=""  
				<?php if ($row_barrios_editar['principal']=1) {echo "checked=\"checked\"";} ?> />
                (Mostrar en Pagina principal)
                <input type="checkbox" name="destacado" value=""  
				<?php if ($row_barrios_editar['destacado']=1) {echo "checked=\"checked\"";} ?> />
                (Mostrar en destacados)
                </td>
              </tr>
              <?php   if($totalRows_paginas > 0 ){ ?>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Paginas:</td>
                <td><table width="100%">
                  <thead>
                   <th>Carcteristica</th>
                    <th colspan="2">Operaciones</th>
                  </thead>
                  
                    <?php while ($row_paginas = mysql_fetch_assoc($paginas)) { ?>
                      <tr>
                        <td ><?php echo $row_paginas['nombre']; ?></td>
                        <td>
                        <a href="barrio_pagina_editar.php?id_caracteristica=<?php echo $row_paginas['id_caracteristica']; ?>">Editar
                        </a></td>
                        <td>
                        <a href="barrios_editar.php?id_barrio=<?php echo $row_barrios_editar['id_barrio']; ?>&id_caracteristica=<?php echo $row_paginas['id_caracteristica']; ?>">Eliminar</a>
                        </td>
                      </tr>
                      <?php } ; ?>
                </table></td>
              </tr>
              <?php } ?>
              <tfoot>
                <tr>
                  <td colspan="5" ><input type="button" onclick="javascript:history.back(1)" style="float:left;margin-right:10px" class="btn" value="Cancelar" />
                    <input type="submit" class="btn" value="Guardar" /></td>
                </tr>
              </tfoot>
            </table>
            <input type="hidden" name="id_barrio" value="<?php echo $row_barrios_editar['id_barrio']; ?>" />
            <input type="hidden" name="MM_update" value="form2" />
            <input type="hidden" name="id_barrio" value="<?php echo $row_barrios_editar['id_barrio']; ?>" />
          </form>
          <p>&nbsp;</p>
        </div>
        
   	  </div>
</div>
</div>
</div>
<?php include('footer.php'); ?>
</body>
</html>
<?php
mysql_free_result($barrios_editar);

mysql_free_result($paginas);


?>
